/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package edu.pku.sei.AMT.impl;

import edu.pku.sei.AMT.AmtPackage;
import edu.pku.sei.AMT.Key;
import edu.pku.sei.AMT.Rule;
import edu.pku.sei.AMT.Transformation;
import edu.pku.sei.AMT.TypedModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Transformation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link edu.pku.sei.AMT.impl.TransformationImpl#getParameters <em>Parameters</em>}</li>
 *   <li>{@link edu.pku.sei.AMT.impl.TransformationImpl#getRules <em>Rules</em>}</li>
 *   <li>{@link edu.pku.sei.AMT.impl.TransformationImpl#getKeys <em>Keys</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class TransformationImpl extends NamedElementImpl implements Transformation {
	/**
	 * The cached value of the '{@link #getParameters() <em>Parameters</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getParameters()
	 * @generated
	 * @ordered
	 */
	protected EList parameters;

	/**
	 * The cached value of the '{@link #getRules() <em>Rules</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRules()
	 * @generated
	 * @ordered
	 */
	protected EList rules;

	/**
	 * The cached value of the '{@link #getKeys() <em>Keys</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getKeys()
	 * @generated
	 * @ordered
	 */
	protected EList keys;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TransformationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EClass eStaticClass() {
		return AmtPackage.Literals.TRANSFORMATION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList getParameters() {
		if (parameters == null) {
			parameters = new EObjectContainmentEList(TypedModel.class, this, AmtPackage.TRANSFORMATION__PARAMETERS);
		}
		return parameters;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList getRules() {
		if (rules == null) {
			rules = new EObjectContainmentEList(Rule.class, this, AmtPackage.TRANSFORMATION__RULES);
		}
		return rules;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList getKeys() {
		if (keys == null) {
			keys = new EObjectContainmentEList(Key.class, this, AmtPackage.TRANSFORMATION__KEYS);
		}
		return keys;
	}

	private HashMap<EClass, EList<Key>> keyMap = new HashMap<EClass,EList<Key>>();
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList getKey(EClass type) {
		EList<Key> result = keyMap.get(type);
		if(result==null) {
			result = new BasicEList<Key>();
			for(Object k : this.getKeys()) {
				Key kk = (Key)k;
				if(kk.getClassName().equals(type.getName()))
					result.add(kk);
			}
			keyMap.put(type, result);
		}
		
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case AmtPackage.TRANSFORMATION__PARAMETERS:
				return ((InternalEList)getParameters()).basicRemove(otherEnd, msgs);
			case AmtPackage.TRANSFORMATION__RULES:
				return ((InternalEList)getRules()).basicRemove(otherEnd, msgs);
			case AmtPackage.TRANSFORMATION__KEYS:
				return ((InternalEList)getKeys()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case AmtPackage.TRANSFORMATION__PARAMETERS:
				return getParameters();
			case AmtPackage.TRANSFORMATION__RULES:
				return getRules();
			case AmtPackage.TRANSFORMATION__KEYS:
				return getKeys();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case AmtPackage.TRANSFORMATION__PARAMETERS:
				getParameters().clear();
				getParameters().addAll((Collection)newValue);
				return;
			case AmtPackage.TRANSFORMATION__RULES:
				getRules().clear();
				getRules().addAll((Collection)newValue);
				return;
			case AmtPackage.TRANSFORMATION__KEYS:
				getKeys().clear();
				getKeys().addAll((Collection)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void eUnset(int featureID) {
		switch (featureID) {
			case AmtPackage.TRANSFORMATION__PARAMETERS:
				getParameters().clear();
				return;
			case AmtPackage.TRANSFORMATION__RULES:
				getRules().clear();
				return;
			case AmtPackage.TRANSFORMATION__KEYS:
				getKeys().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case AmtPackage.TRANSFORMATION__PARAMETERS:
				return parameters != null && !parameters.isEmpty();
			case AmtPackage.TRANSFORMATION__RULES:
				return rules != null && !rules.isEmpty();
			case AmtPackage.TRANSFORMATION__KEYS:
				return keys != null && !keys.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Rule getRuleByName(String name) {
		for(Object r : getRules()) {
			Rule rr = (Rule)r;
			if(rr.getName().equals(name))
				return rr;
		}
		return null;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EList getRulesByModifiers(EList modifiers) {
		EList result = new BasicEList();
		for(Object r : getRules()) {
			Rule rr = (Rule)r;
			if(rr.getModifiers().containsAll(modifiers))
				result.add(rr);
		}
		
		return result;
	}


	public TypedModel getParameter(String name) {
		for(Object o : getParameters()) {
			if(((TypedModel)o).getName().equals(name))
				return (TypedModel)o;
		}
		return null;
	}

} //TransformationImpl
